package com.beiding.sb.fram;

import com.beiding.sb.context.Context;
import com.beiding.sb.handler.TextHandler;

import java.util.LinkedList;
import java.util.List;

//上下文
public class SimpleBuilderContext implements Context {

    //有序列表
    private List<TextHandler> handlers = new LinkedList<>();

    @Override
    public TextHandler getTextHandler(Object obj) {
        for (TextHandler handler : handlers) {
            if (handler.canHandle(obj)) {
                return handler;
            }
        }
        return null;
    }

    public void addHandler(TextHandler handler) {
        this.handlers.add(handler);
    }

    public void addHandler(TextHandler textHandler, int ind) {
        this.handlers.add(ind, textHandler);
    }

    public void removeHandler(TextHandler handler) {
        this.handlers.remove(handler);
    }

}
